//
//  GQHBusinessController.m
//  Xanthium
//
//  Created by GuanQinghao on 20/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHBusinessView.h"

#pragma mark Controller
#import "GQHBusinessController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHBusinessController () <GQHBusinessViewDelegate> {
    
#pragma mark Data
    /// 返回的业务类型数组
    NSMutableArray *businessArray;
#pragma mark View
    /// 自定义根视图
    GQHBusinessView *rootView;
}

@end


@implementation GQHBusinessController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHBusinessView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchServiceData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"选择业务类型";
    [self.qh_rightButton setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *businessDictionary = businessArray[indexPath.row];
    NSString *businessID = [businessDictionary objectForKey:@"key"];
    NSString *businessName = [businessDictionary objectForKey:@"value"];
    self.qh_businessBlock(businessID, businessName);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
/// 获取服务器业务类型参数
- (void)fetchServiceData {
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/common/1/getCodeMap"];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"B1001" forKey:@"type"];
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            businessArray = [data objectForKey:@"beanList"];
            rootView.qh_businessArray = businessArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
